import wx

class NucleotideValidator(wx.PyValidator):
    """Class for string validation."""
    def __init__(self):
        wx.PyValidator.__init__(self)

        # Build it as part of the class and not per Validate() call
        self.allowedInput = "actgrykmswbdhvnuUACTGRYKMSWBDHVN"
        self.Bind(wx.EVT_CHAR, self.onChar)

    def Clone(self):
        return NucleotideValidator()
    
    def Validate(self, win):
        tc = self.GetWindow()
        val = tc.GetValue()

        retVal = True
        for x in val:
            if x not in self.allowedInput:
                retVal = False
                break

        return retVal
    
    def onChar(self, event):
        key = event.GetKeyCode()
        if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255:
            event.Skip()

        elif chr(key) in self.allowedInput:
            event.Skip()

        else:
            if not wx.Validator_IsSilent():
                wx.Bell()
        return